require(here)
require(reshape2)
require(dplyr)
require(ggplot2)

#########################################################################
##Replication Materials (Institutional Performance) #####################
##The Institutional Origins of China's International Crises #############
#########################################################################

#########################################################################
##Institutional Performance##############################################
#########################################################################

d <- read.csv("prc_crisis_data (by crisis).csv")
d2 <- read.csv("prc_crisis_data (by year).csv")
d3 <- read.csv("personalism (by year).csv")
d4 <- read.csv("personalism (by crisis).csv")

##Introduction: Total Number of Crises with Inaccurate Assessments w/Costs
nrow(d[d$inaccurate==1 & is.na(d$inaccurate)==F & d$cost==1,]) / nrow(d)

##Total Number of Crisis with Inaccurate Assessment
nrow(d[d$inaccurate==1 & is.na(d$inaccurate)==F,]) #15

##Total Number of Crises that Failed to Achieve Primary Goal
nrow(d[d$primary_fail==1,]) #12

##Inaccurate Assessments
nrow(d[d$type=="Integrated" & d$inaccurate==1,]) #2
nrow(d[d$type=="Integrated",]) #9
nrow(d[d$type=="Fragmented" & d$inaccurate==1,]) #8
nrow(d[d$type=="Fragmented",]) #8
nrow(d[d$type=="Siloed" & d$inaccurate==1 & is.na(d$inaccurate)==F,]) #5
nrow(d[d$type=="Siloed",]) #9

t.test(d$inaccurate[d$type=="Integrated"], d$inaccurate[d$type=="Fragmented"])
t.test(d$inaccurate[d$type=="Integrated"], d$inaccurate[d$type=="Siloed"])

##Achieved Primary Goal
nrow(d[d$type=="Integrated" & d$primary_fail==0,]) #8
nrow(d[d$type=="Integrated",]) #9
nrow(d[d$type=="Fragmented" & d$primary_fail==0,]) #2
nrow(d[d$type=="Fragmented",]) #8
nrow(d[d$type=="Siloed" & d$primary_fail==0,]) #4
nrow(d[d$type=="Siloed",]) #9

t.test(d$primary_fail[d$type=="Integrated"], d$primary_fail[d$type=="Fragmented"])
t.test(d$primary_fail[d$type=="Integrated"], d$primary_fail[d$type=="Siloed"])

##Share of Goals Achieved
##Aggreate by Institutional Type
d.agg <- aggregate(pct_fail ~ type, d, mean)
##Average Achieved under Integrated
round(1-d.agg$pct_fail[d.agg$type=="Integrated"],2) #89 percent
##Average Achieved under Fragmented
round(1-d.agg$pct_fail[d.agg$type=="Fragmented"],2) #36 percent
##Average Achieved under Siloed
round(1-d.agg$pct_fail[d.agg$type=="Siloed"],2) #41 percent

t.test(d$pct_fail[d$type=="Integrated"], d$pct_fail[d$type=="Fragmented"])
t.test(d$pct_fail[d$type=="Integrated"], d$pct_fail[d$type=="Siloed"])

##Assessment Accuracy and Achieving Crisis Goals
round(1 - mean(d$pct_fail[d$inaccurate==0 & is.na(d$inaccurate)==F]),2) #85 percent
round(1 - mean(d$pct_fail[d$inaccurate==1 & is.na(d$inaccurate)==F]),2) #37 percent

##Relative Costliness of Inaccurate Assessments (Siloed and Fragmented Institutions Only)
nrow(d[d$type=="Fragmented" & d$inaccurate==1 & d$cost==1,]) #7
nrow(d[d$type=="Fragmented" & d$inaccurate==1,]) #8
nrow(d[d$type=="Siloed" & d$inaccurate==1 & d$cost==1,]) #1
nrow(d[d$type=="Siloed" & d$inaccurate==1,]) #6

t.test(d$cost[d$type=="Siloed" & d$inaccurate==1], d$cost[d$type=="Fragmented" & d$inaccurate==1])

##Overall Predictive Success of Theory
nrow(d[(d$type=="Integrated" & d$inaccurate==0 & d$primary_fail==0 & is.na(d$name)==F) |
  (d$type=="Siloed" & d$inaccurate==1 & d$primary_fail==1 & d$cost==0 & is.na(d$name)==F) |
  (d$type=="Fragmented" & d$inaccurate==1 & d$primary_fail==1 & d$cost==1 & is.na(d$name)==F),]) #17

##Frequency of Crisis Failure (by year)
d2.agg <- aggregate(crisis_miscalculation ~ type, d2, mean)
##Integrated
round(1/d2.agg$crisis_miscalculation[d2.agg$type=="Integrated"]) #Once every fourteen years
##Fragmented
round(1/d2.agg$crisis_miscalculation[d2.agg$type=="Fragmented"]) #Once every three years
##Siloed
round(1/d2.agg$crisis_miscalculation[d2.agg$type=="Siloed"]) #Once every six years

#########################################################################
##Personalism Analysis in Main Text######################################
#########################################################################

##Miscalculations under Personalist/Non-Personalist Rule w/Mao's Death as break (Weeks 2014)
nrow(d4[d4$weeks_bin==1 & d4$inaccurate==1,]) #7
nrow(d4[d4$weeks_bin==1,]) #14
nrow(d4[d4$weeks_bin==0 & d4$inaccurate==1,]) #9
nrow(d4[d4$weeks_bin==0,]) #12

#########################################################################
##Personalism Analysis (Appendix Section 5)##############################
#########################################################################

##Generate Appendix Figure 1 
d3.f <- reshape2::melt(d3[c(1:4)], id.vars="year")
d3.f$variable <- as.factor(d3.f$variable)
d3.f$variable <- recode(d3.f$variable, jost = "Author", weeks = "Weeks (2012)", geddes = "Geddes et al. (2017)")
d3.f$variable <- relevel(d3.f$variable, ref = "Geddes et al. (2017)")
d3.f$variable <- relevel(d3.f$variable, ref = "Weeks (2012)")
p1 <- ggplot(d3.f, aes(x=year, y=value, group=variable)) +
  geom_line(aes(color=variable)) + ylim(0,1) +
  theme_minimal() + xlab("") + ylab("Personalism Score") + ggtitle("") +
  scale_color_manual(values=c("firebrick","darkblue", "darkgreen")) +
  geom_vline(xintercept=1966, colour="black", linetype="dashed") +
  geom_vline(xintercept=1976, colour="black", linetype="dashed") +
  annotate("text", x=1967.5, y=0.35, label="Cultural Revolution Begins", angle=90) +
  annotate("text", x=1975, y=0.55, label="Mao's Death", angle=90) +
  theme(legend.position="bottom", legend.margin=margin(-12.5,0,0,0), legend.text = element_text(face="bold"),
        axis.title.y = element_text(face="bold"), plot.title = element_text(face="bold", hjust = 0.5),
        legend.title=element_blank())
p1

##Personalism - Geddes et al. (Continuous)
summary(aov(inaccurate ~ geddes, data = d4)) #p=0.358
summary(aov(primary_fail ~ geddes, data = d4)) #p=0.579

##Personalism - Weeks (Continuous)
summary(aov(inaccurate ~ weeks, data = d4)) #p=0.350
summary(aov(primary_fail ~ weeks, data = d4)) #p=0.090

##Personalism - Jost
summary(aov(inaccurate ~ jost, data = d4)) #p=0.863
summary(aov(primary_fail ~ jost, data = d4)) #p=0.21
